﻿// $Id: ColumnIdentifierTypeConverter.cs 64 2008-05-06 18:29:47Z nvivo $

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;

namespace DBLGen.UI.Design
{
	public class ColumnIdentifierTypeConverter : StringConverter
	{
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if (context.Instance is Method)
			{
				Method m = (Method)context.Instance;
				string propertyName = context.PropertyDescriptor.Name;

				// validates empty column name
				if (propertyName == "Name" && String.IsNullOrEmpty((string)value))
					throw new InvalidIdentifierException("The column name cannot be empty.");

				// validates columns with the same name on the table
				if (m.Table != null && m.Table.Columns.Find((string)value) != null)
					throw new InvalidIdentifierException("There is a column with this name on the table already.");
			}
			else
			{
				Column c = (Column)context.Instance;
				string propertyName = context.PropertyDescriptor.Name;

				// validates empty column name
				if (propertyName == "Name" && String.IsNullOrEmpty((string)value))
					throw new InvalidIdentifierException("The column name cannot be empty.");

				// validates columns with the same name on the table
				if (c.Table != null && c.Table.Columns.Find((string)value) != null)
					throw new InvalidIdentifierException("There is a column with this name on the table already.");
			}
			return base.ConvertFrom(context, culture, value);
		}
	}
}
